package com.yixinsoft.basic.web.controller.manage.system;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.github.pagehelper.PageInfo;
import com.yixinsoft.basic.common.Constants;
import com.yixinsoft.basic.model.manage.system.Role;
import com.yixinsoft.basic.model.manage.system.User;
import com.yixinsoft.basic.service.manage.system.OrganizationService;
import com.yixinsoft.basic.service.manage.system.RoleService;
import com.yixinsoft.basic.service.manage.system.UserService;

/**
 * Created by hqy on 2016/06/05
 */
@Controller
@RequestMapping("/manage/system/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private OrganizationService organizationService;
	
	@Autowired
	private RoleService roleService;
	
	@RequiresPermissions("user:view")
	@RequestMapping
	public String list(Model model,
			@RequestParam(required = false, defaultValue = Constants.PAGENUM)Integer p,
			@RequestParam(required = false, defaultValue = Constants.PAGESIZE)Integer ps,
			String username) throws Exception {
		User user = null;
		if (!StringUtils.isEmpty(username)) {
			user = new User();
			user.setUsername(username);
			model.addAttribute("username", username);
		}
		PageInfo<User> pageInfo = userService.queryListPage(user, p, ps);
		model.addAttribute("pageInfo", pageInfo);
		return "manage/system/user/list";
	}
	
	@RequiresPermissions("user:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(Model model) {
		setCommonData(model);
		model.addAttribute("user", new User());
		return "manage/system/user/create";
	}
	
	@RequiresPermissions("user:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String create(Model model, User user, RedirectAttributes redirectAttributes) {
		userService.save(user);
		redirectAttributes.addFlashAttribute("msg", "新增成功");
		return "redirect:/manage/system/user";
	}
	
	@RequiresPermissions("user:update")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String update(Model model, @PathVariable Long id) {
		setCommonData(model);
		model.addAttribute("user", userService.queryById(id));
		return "manage/system/user/edit";
	}
	
	@RequiresPermissions("user:update")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
	public String update(Model model, User user, RedirectAttributes redirectAttributes) {
		userService.update(user);
		redirectAttributes.addFlashAttribute("msg", "修改成功");
		return "redirect:/manage/system/user";
	}
	
	@RequiresPermissions("user:delete")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public String delete(Long[] ids, RedirectAttributes redirectAttributes) {
		userService.deleteByIds(ids);
		redirectAttributes.addFlashAttribute("msg", "删除成功");
		return "redirect:/manage/system/user";
	}
	
	@RequiresPermissions("user:update")
	@RequestMapping(value = "/changePassword/{id}", method = RequestMethod.GET)
	public String changePassword(Model model, @PathVariable Long id) {
		model.addAttribute("user", userService.queryById(id));
		return "manage/system/user/changePassword";
	}
	
	@RequiresPermissions("user:update")
	@RequestMapping(value = "/changePassword/{id}", method = RequestMethod.POST)
	public String changePassword(@PathVariable Long id, String newPassword, RedirectAttributes redirectAttributes) {
		userService.changePassword(id, newPassword);
		redirectAttributes.addFlashAttribute("msg", "修改密码成功");
		return "redirect:/manage/system/user";
	}
	
	@RequiresPermissions("user:update")
	@RequestMapping(value = "/updateState", method = RequestMethod.POST)
	public String updateState(Long[] ids, Boolean state, RedirectAttributes redirectAttributes) {
		if (!ObjectUtils.isEmpty(ids)) {
			userService.updateState(ids, state);
			redirectAttributes.addFlashAttribute("msg", "操作成功");
		}
		return "redirect:/manage/system/user";
	}
	
	@RequiresPermissions("user:create")
	@RequestMapping(value = "/findByUsername", method = RequestMethod.POST)
	@ResponseBody
	public String findByUsername(String username) {
		User user = null;
		if (!StringUtils.isEmpty(username)) {
			user = userService.findByUsername(username);
		}
		return user != null ? Boolean.TRUE.toString() : Boolean.FALSE.toString();
	}
	
	private void setCommonData(Model model) {
		model.addAttribute("organizationList", organizationService.queryList(null));
		model.addAttribute("roleList", roleService.queryList(new Role()));
	}
}
